package com.holyant.goods.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: tswsoft
 * @Date: 2021/04/10/10:56 AM
 * @Description:
 */
public class ExtAttrUtils {

    /**
     * 商品或sku扩展属性转换显示
     * @param extAttr
     * @return
     */
    public static String getExtAttrDisplay(String extAttr) {
        if (StringUtils.isNotEmpty(extAttr)) {
            List<String> list = new ArrayList<>();
            JSONArray jsonArray = JSON.parseArray(extAttr);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                if(StringUtils.isNotEmpty(obj.getString("value"))){
                    list.add(obj.getString("name") + ":" + obj.getString("value"));
                }
            }
            return String.join(",", list);
        }
        return "";

    }
}
